﻿using System.Text.Json;

namespace WebApplication1.Tools
{
    public static class JsonParser
    {
        public static JsonDocument GetJsonDocument(string jsonString)
        {
            return JsonDocument.Parse(jsonString);
        }

        public static string? GetPropertyValue(this JsonDocument doc, string propertyName)
        {
            var root = doc.RootElement;
            string[] propertyNames = propertyName.Split('.');
            foreach (var name in propertyNames)
            {
                if (root.TryGetProperty(name, out JsonElement element))
                {
                    if (element.ValueKind == JsonValueKind.Object || element.ValueKind == JsonValueKind.Array)
                    {
                        root = element;
                    }
                    else
                    {
                        return element.ToString();
                    }
                }
                else
                {
                    return null;
                }
            }
            return null;
        }
    }
}
